package com.caidan.search.controller;

import cn.hutool.core.util.StrUtil;
import com.caidan.common.result.Result;
import com.caidan.common.result.ResultCodeEnum;
import com.caidan.common.util.AuthContextHolder;
import com.caidan.search.service.BingSearchService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @项目名 CaiDanSearchSystem
 * @描述 必应爬虫接口
 * @作者 小菜蛋
 * @时间 2024/12/4 10:00
 */

@RestController
@Slf4j
@RequestMapping("/bing")
public class BingSearchController {


    @Resource
    private BingSearchService bingSearchService;

    @GetMapping("/search")
    public Result bingSearch(String param) {

        if(StrUtil.isBlank(param)){
//            参数校验失败
            return Result.fail(ResultCodeEnum.ARGUMENT_VALID_ERROR.getMessage());
        }
        Long userId = AuthContextHolder.getUserId();
//        非用户，一次搜索两个请求
        if (null == userId) {
            return bingSearchService.bingSearch(userId,false,param);
        }else {
            return bingSearchService.bingSearch(userId,true,param);
        }
    }
}
